﻿
using LxCFD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace TimeSolve
{
    public class LogWin : LxConsole.IConsole
    {
        public LogWin(RichTextBox richtextbox, int maxLines, double LineHeight)
        {
            this.richtextbox = richtextbox;
            this.maxLines = maxLines;
            this.LineHeight = LineHeight;
            this.richtextbox.Document.Blocks.Clear();
            LxConsole.SetConsole(this);
        }
        public LogWin(RichTextBox richtextbox)
        {
            this.richtextbox = richtextbox;
            this.richtextbox.Document.Blocks.Clear();
            LxConsole.SetConsole(this);
        }
        double offsetLog = 0;
        int maxLines = 50;
        double LineHeight = 3;
        RichTextBox richtextbox;
        TaskScheduler uitask = TaskScheduler.FromCurrentSynchronizationContext();
        //Func<string, string> strFormat = (s) => string.Format("{0} :  {1}", DateTime.Now, s);

        protected void AddWithTime(string msg, Brush color)
        {
            msg = string.Format("{0} :  {1}", DateTime.Now, msg);
            Add(msg, color);
        }
        public void AddWithTime(string msg)
        {
            AddWithTime(msg, Brushes.Black);
        }
        protected void Add(string msg, Brush color)
        {
            Paragraph p = new Paragraph(new Run(msg));
            p.Foreground = color;
            p.LineHeight = LineHeight;
            richtextbox.Document.Blocks.Add(p);
            if (richtextbox.Document.Blocks.Count > maxLines)
            {
                for (int i = maxLines; i < richtextbox.Document.Blocks.Count; i++)
                {
                    richtextbox.Document.Blocks.Remove(richtextbox.Document.Blocks.FirstBlock);
                }
            }

            if (richtextbox.VerticalOffset >= offsetLog)
            {
                richtextbox.ScrollToEnd();
                offsetLog = richtextbox.VerticalOffset;
            }
        }
        public void Add(string msg)
        {
            Add(msg, Brushes.Black);
        }

        protected void WriteLine(bool isWithTime, string message)
        {
            if (richtextbox.Dispatcher.CheckAccess())
            {
                console();
            }
            else
            {
                Task t = Task.Factory.StartNew(console, new CancellationTokenSource().Token, TaskCreationOptions.None, uitask);
                t.Wait();
            }
            void console()
            {
                if (isWithTime)
                {
                    AddWithTime(message);
                }
                else
                {
                    Add(message);
                }
            }
        }
        protected void write()
        {

        }
        public void Calc(string stringMessage)
        {
            WriteLine(false, stringMessage);
        }
        public void Info(string stringMessage)
        {
            WriteLine(true, stringMessage);
        }
        public void Warn(string stringMessage)
        {
            WriteLine(true, stringMessage);
        }
        public void Error(string stringMessage)
        {
            WriteLine(true, stringMessage);
        }
    }
}

//public void Rewrite(string msg, Brush color)
//{
//    richtextbox.Document.Blocks.Remove(richtextbox.Document.Blocks.LastBlock);
//    Add(msg, color);
//}
//public void Rewrite(string msg)
//{
//    Rewrite(msg, richtextbox.Document.Blocks.LastBlock.Foreground);
//}